using UnityEngine;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf;

//[System.Serializable]
[ProtoContract]
public class NPC_Stats {
	[ProtoMember(1)]	public List<Spell_Power_Type> Resistances_Types {get; set;}
	[ProtoMember(2)]	public List<int> Resistance_Values {get; set;}

	[ProtoMember(3)]	public List<Spell_Power_Type> Dmg_Types{get; set;}
	[ProtoMember(4)]	public List<int> Dmg_Values{get; set;}

	[ProtoMember(5)]	public List<int> Spells{get; set;}
	[ProtoMember(6)]	public int Life_Cap{get; set;}
	[ProtoMember(7)]	public int Energy_Cap{get; set;}
	[ProtoMember(8)]	public int Id{get; set;}
	[ProtoMember(9)]	public string Name{get; set;}
	[ProtoMember(10)]	public string Object_Name{get; set;}
	[ProtoMember(11)]	public List<string> Spell_Name{get; set;}
	
	public NPC_Stats(){
		Name = "NPC Name";
		Resistances_Types = new List<Spell_Power_Type>();
		Resistance_Values= new List<int>();
		
		Spells = new List<int>();
		
		Dmg_Types = new List<Spell_Power_Type>();
		Dmg_Values = new List<int>();
		Spell_Name = new List<string>();
	}
	
	public NPC_Stats(NPC_Stats clone){
		Resistances_Types = clone.Resistances_Types;
		Resistance_Values = clone.Resistance_Values;
		Dmg_Types = clone.Dmg_Types;
		Dmg_Values = clone.Dmg_Values;
		Spells = clone.Spells;
		Life_Cap = clone.Life_Cap;
		Energy_Cap = clone.Energy_Cap;
		Id=clone.Id;
		Name=clone.Name;
		Object_Name=clone.Object_Name;
		Spell_Name = clone.Spell_Name;
	}
	
	/*public void Save( string filename )
	{
	    using (var file = File.Create(filename+".ncol")) {
    		Serializer.Serialize(file, this);
		}
	}*/
	
	public void Save( FileStream sm )
	{
		using(MemoryStream ms = new MemoryStream()){
	   		Serializer.Serialize(ms, this);
			long psize = ms.Position;
			sm.Write(BitConverter.GetBytes(psize),0,4);
			ms.WriteTo(sm);
			Debug.Log("ISave - " + psize + "/" + sm.Position.ToString());

		}
	}
	
	public void Load( FileStream sm )
	{
		long psize = IOHelper.ReadLong(sm);
		Byte[] bf = new Byte[psize];
		sm.Read(bf,0,(int)psize);
		using(MemoryStream ms = new MemoryStream(bf)){
			NPC_Stats tmp = Serializer.Deserialize<NPC_Stats>(ms);
			Resistances_Types = tmp.Resistances_Types;
			Resistance_Values = tmp.Resistance_Values;
			Dmg_Types = tmp.Dmg_Types;
			Dmg_Values = tmp.Dmg_Values;
			Spells = tmp.Spells;
			Life_Cap = tmp.Life_Cap;
			Energy_Cap = tmp.Energy_Cap;
			Id=tmp.Id;
			Name=tmp.Name;
			Object_Name=tmp.Object_Name;
			Spell_Name = tmp.Spell_Name;
			Debug.Log("ILoad - " + psize.ToString()+ "/ "+ sm.Position.ToString() );
		}
	}
}
